<?php
namespace app\www\controller;
use x\my\Captcha;
use x\Xbase;
class Publics extends Base{
    //短信提示
    public function sendSms(){
        if(is_post()){
            $d = $this->post();
            $rule = [
                'type'=>'require',
                'phone'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $s       = m('Sms','xcom');
            $phone  = $d['phone'];
            $code    = rand(1000,9999);
            $type    = $d['type'];
            $ext = isset($d['ext']) ? $d['ext'] : '';
            switch ($type) {
                #用户注册
                case 'register':
                    if(!$ext){
                        err('请填写用户名称');
                    }
                    $token = cookie('captcha_token');
                    if( !Captcha::checkCaptcha($token,$d['imgcode']) ){
                        err('图形验证码错误!');
                    }
                    $rs = $this->db->query('select id from user where username=?',[$ext],1);
                    if($rs){
                        err('用户名已存在!');
                    }
                    $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                    if($rs){
                        err('该手机号已存在!');
                    }
                    $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                    $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                    $no = 2247074;
                    break;
                #忘记密码
                case 'forget':
                    $token = cookie('captcha_token');
                    if( !Captcha::checkCaptcha($token,$d['code']) ){
                        err('图形验证码错误!');
                    }
                    $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                    if(!$rs){
                        err('该手机号不存在!');
                    }
                    $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                    $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                    $no = 2247074;
                    break;
                #绑定新号码
                case 'newtel':
                    $rs = $this->db->query('select id from user where phone=?',[$phone],1);
                    if($rs){
                        err('该手机号已存在!');
                    }
                    $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                    $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                    $no = 2247074;
                    break;
                #重置密码
                case 'remake':
                    $rs = $this->db->query('select id,password from user where phone=?',[$phone],1);
                    if(!$rs){
                        err('该手机号不存在!');
                    }
                    if($rs["password"] != com_password($d['oldpassword'])){
                        err('原密码错误!');
                    }
                    $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                    $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                    $no = 2247074;
                    break;
                default:
                    $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                    $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                    $no = 2247074;
                    break;
            }
            $id = $s->create($phone, $content, $type, $code, $no, $tpl_val);
            if(!$id){
                err($s->getError());
            }
            if($s->sendOne($id)){
                $rs = $this->db->query('select token from sms_token where sms_history_id=?',[$id],1);
                suc(['token'=>$rs['token']]);
            }else{
                err($s->getError());
            }
        }
    }

    //获取子地区名称
    public function getComAddress(){
        $pid = $this->post('pid') ? $this->post('pid') : $this->get('pid');
        $rs = $this->db->query('select id,name from com_address where parent_id=?',[$pid]);
        suc($rs);
    }

    //获取子地区名称
    public function getAllAddress(){
        $rs = $this->db->query('select id,name from com_address');
        $map = [];
        foreach ($rs as $key => $value) {
            $map[$value['id']] = $value['name'];
        }
        return $map;
    }


    //登录界面
    public function login(){
        if(is_post()){
            $d = $this->post;
            $token = cookie('captcha_token');
            if( !Captcha::checkCaptcha($token,$d['code']) ){
                cookie('js_name','code');
                err('验证码错误!');
            }
            $rule = [
                'username'=>'require',
                'password'=>'require',
            ];
            $msg = [
                'username.require' => '请输入用户名',
                'password.require' => '请输入密码',
            ];
            check($rule,$msg,$d);
            $rs = m('User','c')->login($d['username'],$d['password']);
            if($rs){
                cookie('js_url',url('index/init'));
                cookie('js_msg','登录成功!');
                return suc();
            }
            err(m('User','c')->getError());
        }
        $this->fetch([],'',false);
    }

    //验证码
    public function captcha(){
        $token = token();
        cookie('captcha_token',$token);
        return Captcha::generate($token);
    }
    //退出
    public function logout(){
        $token = cookie('token');
        if($token){
            $this->db->update('user',['token'=>''],['token=?',[$token]]);
        }
        cookie(null,null);
        r(url('publics/login'));
    }

    //注册
    public function register(){
        if(is_post()){
            $d = $this->post;
            //判断短信验证码
            $rule = [
                'username'=>'require',
                'userphone'=>'require',
                'password'=>'require',
                'code'=>'require',
                'agreement' => 'require',
            ];
            $msg = [
                'username.require' => '请输入用户名',
                'userphone.require' => '请输入手机号',
                'password.require' => '请输入密码',
                'code.require' => '请输入验证码',
                'agreement.require' => '请同意协议',
            ];
            check($rule,$msg,$d);
            //短信验证
            if(!m('sms','xcom')->checkCode($d['userphone'], 'register', $d['code'])){
                err(m('sms','xcom')->getError());
            }
            $rs = m('User','c')->register($d['username'],$d['userphone'],$d['password']);
            if($rs){
                cookie('js_url',url('Publics/login'));
                cookie('js_msg','注册成功!');
                return suc();
            }
            err(m('User','c')->getError());
        }
        $this->fetch([],'',false);
    }

    //忘记密码
    public function forget(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'userphone'=>'require',
                'password'=>'require',
                'code'=>'require',
            ];
            $msg = [
                'userphone.require' => '请输入手机号',
                'password.require' => '请输入新密码',
                'code.require' => '请输入验证码',
            ];
            check($rule,$msg,$d);
            //短信验证
            if(!m('sms','xcom')->checkCode($d['userphone'], 'forget', $d['code'])){
                err(m('sms','xcom')->getError());
            }
            $rs = m('User','c')->forget($d['userphone'],$d['password']);
            if($rs){
                cookie('js_url',url('Publics/login'));
                cookie('js_msg','重置成功!');
                return suc();
            }
            err(m('User','c')->getError());
        }
        $this->fetch([],'',false);
    }

    public function test(){
        jump('恭喜你成为会员',url('publics/login'));
        // $this->db->begin();
        // $mysql = [];
        // $mysql[] = $this->db->insert('temp_id',['t'=>1]);
        // $mysql[] = false;
        // $rs = $this->db->end($mysql);
        // if($rs){
        //     t('插入');
        // }else{
        //     t('未插入');
        // }
    }

}
?>
